import { bus } from "/utils/core";
import { platformList } from "/utils/dictionary";
import validator from "/utils/validators";
import { provinceCityArea } from "/utils/mixins";
const app = getApp();

Component({
  mixins: [provinceCityArea],
  data: {
    permissions: [], // 权限组
    userInfo: null, // 用户信息
    show: false,
    platformList,
    modifyIndex: null,
    form: {
      name: "",
      areaIndexs: [],
      provinceId: null,
      cityId: null,
      districtId: null,
      detailAddress: "",
      contacts: "",
      contactsPhone: "",
      shopPlatformList: []
    }
  },
  props: {
    shopList: [],
    onSubmit: () => { },
    initStore: () => { }
  },
  didMount() {
    this.setData({
      permissions: app.globalData.permissions,
      userInfo: app.globalData.userInfo
    });
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    async modifyStoreForm(e) {
      let storeForm = JSON.parse(JSON.stringify(this.props.shopList[e.target.targetDataset.index]));
      this.setData({
        show: true
      });
      if (storeForm.id) {
        dd.showLoading({
          content: '加载中...',
        });
        storeForm.shopPlatformList.map(element => {
          element.platformIndex = this.data.platformList.findIndex(item => item.value === element.platform);
        });
        if (storeForm.provinceId) {
          let areaIndexs = [];
          const _this = this;
          areaIndexs[0] = this.data.citys.findIndex(item => item.id === storeForm.provinceId);
          if (!this.data.citys[areaIndexs[0]].children) {
            let cityRes = await this.getCityData({
              areaId: storeForm.provinceId,
              type: 1
            });
            this.data.citys[areaIndexs[0]].children = cityRes.voList;
            await getData(areaIndexs);
          } else {
            await getData(areaIndexs);
          }
          async function getData(arr) {
            arr[1] = _this.data.citys[arr[0]].children.findIndex(item => item.id === storeForm.cityId);
            if (!_this.data.citys[arr[0]].children[arr[1]].children) {
              let areaRes = await _this.getCityData({
                areaId: storeForm.cityId,
                type: 2
              })
              _this.data.citys[arr[0]].children[arr[1]].children = areaRes.voList;
            }
            arr[2] = _this.data.citys[arr[0]].children[arr[1]].children.findIndex(item => item.id === storeForm.districtId)
            storeForm.areaIndexs = arr;
          }
        }
        dd.hideLoading();
      }
      Object.assign(this.data.form, storeForm);
      this.setData({
        modifyIndex: e.target.targetDataset.index,
        form: this.data.form,
        citys: this.data.citys
      });
    },
    hideModal() {
      this.setData({
        show: false
      });
    },
    catchMethod() { },
    newPlatform() {
      this.data.form.shopPlatformList.push({
        platform: null,
        platformId: "",
        platformIndex: null
      });
      this.setData({
        "form.shopPlatformList": this.data.form.shopPlatformList
      });
    },
    onProvinceCityChange(arr, [provinceId, cityId, areaId]) {
      this.data.form.provinceId = provinceId;
      this.data.form.cityId = cityId;
      this.data.form.districtId = areaId;
      this.data.form.areaIndexs = arr;
      this.setData({
        form: this.data.form,
        visibleProvinceCityPicker: false
      });
    },
    onPlatformChange(e) {
      this.data.form.shopPlatformList[e.target.targetDataset.index].platformIndex = e.detail.value;
      this.data.form.shopPlatformList[e.target.targetDataset.index].platform = this.data.platformList[e.detail.value].value;
      this.setData({
        "form.shopPlatformList": this.data.form.shopPlatformList
      });
    },
    onPlatformIdChange(e) {
      this.data.form.shopPlatformList[e.target.targetDataset.index].platformId = e.detail.value;
      this.setData({
        "form.shopPlatformList": this.data.form.shopPlatformList
      })
    },
    onPlatformDel(e) {
      dd.confirm({
        title: '提示',
        content: '您确定要删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            this.data.form.shopPlatformList.splice(e.target.targetDataset.index, 1);
            this.setData({
              "form.shopPlatformList": this.data.form.shopPlatformList
            });
          }
        },
      });
    },
    onFormSubmit(e) {
      Object.assign(this.data.form, e.detail.value);
      let validate = new validator();
      validate.add("checkEmpty", this.data.form.name, "门店名称不能为空");
      validate.add("checkEmpty", this.data.form.provinceId, "联系地址不能为空");
      validate.add("checkEmpty", this.data.form.detailAddress, "详细地址不能为空");
      validate.add("checkEmpty", this.data.form.contacts, "联系人不能为空");
      validate.add("checkPhoneNumber", this.data.form.contactsPhone, "手机号码不符合规则");
      validate.add("checkEmptyArray", this.data.form.shopPlatformList, "至少新增一个平台ID");
      validate.add("checkPlatformId", this.data.form.shopPlatformList);
      if (validate.check()) {
        this.props.onSubmit({
          index: this.data.modifyIndex,
          form: this.data.form
        }).then(() => {
          this.setData({
            show: false
          });
          this.props.initStore()
        })
      }
    }
  },
});
